﻿using Dln.Common;
using Dln.Common.Helper;
using Dln.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Page_Common_WeixinRequestPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string code = this.Request.QueryString["CODE"] == null ? "" : this.Request.QueryString["CODE"].ToString();
            string state = this.Request.QueryString["state"] == null ? "" : this.Request.QueryString["state"].ToString();
            string from = this.Request.QueryString["from"] == null ? "" : this.Request.QueryString["from"].ToString();
            if (!string.IsNullOrEmpty(from))
            {
                from = SecurityHelper.DEMD5(from.Replace(" ", "+"));
            }
            string appID = CommonHelper.GetAppSettings("AppID");
            string appSecret = CommonHelper.GetAppSettings("AppSecret");

            #region 验证
            if (string.IsNullOrEmpty(code) || code == "authdeny" || string.IsNullOrEmpty(appID) || string.IsNullOrEmpty(appSecret) || string.IsNullOrEmpty(state))
            {
                Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问1")));
            }
            string json = RequestHelper.SendGet(String.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appID, appSecret, code));
            if (string.IsNullOrEmpty(json))
            {
                Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问2")));
            }
            OAuthConfig oAuthConfig = JSONHelper.FromJson<OAuthConfig>(json);
            if (oAuthConfig == null)
            {
                Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问3")));
            }
            string strOpenId = oAuthConfig.openid;
            if (string.IsNullOrEmpty(strOpenId))
            {
                Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问4")));
            }
            #endregion

            #region 设置cookie
            //设置cookie
            HttpCookie userCookie = HttpContext.Current.Request.Cookies["CookiesOpenid"];
            if (userCookie != null)
            {
                HttpContext.Current.Response.Cookies.Remove("CookiesOpenid");
            }
            userCookie = new HttpCookie("CookiesOpenid");
            userCookie.Values["OpenId"] = strOpenId;
            userCookie.Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.AppendCookie(userCookie);
            #endregion

            switch (state.ToLower())
            {
                #region 登陆系统
                case "memberpassword":
                    Response.Redirect("/page/Community/UserInfo.aspx?from=" + from);
                    break;
                #endregion

                #region 个人中心
                case "membercenter":
                    //Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问"+ userCookie["OpenId"])));
                    Response.Redirect("/page/Community/UserInfo.aspx");
                    break;
                #endregion

                #region workinfo
                case "workInfo":
                    Response.Redirect("/page/DutyAction/WorkInfo.aspx");
                    break;
                #endregion
                case "getopenid":
                     Response.Redirect("/page/AutoRun/Contact.aspx");
                    break;
                case "getopenids":
                    Response.Redirect("/page/AutoUpdate/WeChatStatics.aspx");
                    break;
                default:
                    Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问5")));
                    break;
            }
        }
        catch (System.Threading.ThreadAbortException ex)
        {

        }
        catch (Exception ex)
        {
            Response.Redirect(String.Format("/page/Common/message.aspx?m={0}", Server.UrlEncode("请先关注公众账号德朗诺，且从微信客户端访问6")));
        }

    }
}